$(() => {
	loadData();
	initListener();
})

initListener = () => {
	$("#load_layout").on('click', () => {
		loadData();
	});
	$("#edit_address").on('click', () => {
		navigateTo(`edit_address.html`)
	});

	
}

loadData = () => {
	startLoading();
	$("#list").html('');
	let postData = {};
	setTimeout(() => {
		queryAddressList(res => {
			if (res.code == 200) {
				let data = res.data;
				setList(data);
			} else {
				setLoadState('stateUlr', loginFail());
			}
		})
	},500)
}

setList = (mlist) => {
	$(`#list`).html('');
	if (ArrayEmpty(mlist)) {
		setLoadState('stateUlr', noData());
	} else {
		hideLoading();
		let itemHtml = ``;

		mlist.forEach((e, i) => {
			let firstStr = e.name.substring(0, 1);
			let provinceCity = e.province + e.city;

			let labelItem = ``;
			if (e.defaultAdd) {
				labelItem = `<span class="label">默认</span>`;
			} else {
				labelItem = `<span class="normal">${e.label}</span>`;
			}

			itemHtml += `<div class="item">
								<a class="address_ceil">
									<div class="address_body">
									
										<div class="address_list">
											<div class="address_letter">${firstStr}</div>
											<div class="address_content">
												<div class="province">${provinceCity}</div>
												<div class="address">${e.address}</div>
												<div class="user">
													<span>${e.name}</span>
													<span >${e.phone}</span>
													${labelItem}
												</div>
											</div>
											<img src="../../img/ic_edit.png" class="address_edit_icon" onclick="getItem('${toObjectStr(e)}')"/>
										</div>
									</div>
									<div class="swipe_btn" onclick="getDeleteItem('${e.id}')">删除</div>
								</a>
							</div>`;


		})
		$(`#list`).html(itemHtml);
	}
	
	
	//侧滑显示删除按钮
	var expansion = null; //是否存在展开的list
	var container = document.querySelectorAll('.address_ceil');
	for (var i = 0; i < container.length; i++) {
		var x, y, X, Y, swipeX, swipeY;
		container[i].addEventListener('touchstart', function(event) {
			x = event.changedTouches[0].pageX;
			y = event.changedTouches[0].pageY;
			swipeX = true;
			swipeY = true;
			if (expansion) { //判断是否展开，如果展开则收起
				expansion.className = "";
			}
		});
		container[i].addEventListener('touchmove', function(event) {
	
			X = event.changedTouches[0].pageX;
			Y = event.changedTouches[0].pageY;
			// 左右滑动
			if (swipeX && Math.abs(X - x) - Math.abs(Y - y) > 0) {
				// 阻止事件冒泡
				event.stopPropagation();
				if (X - x > 10) { //右滑
					event.preventDefault();
					this.className = ""; //右滑收起
				}
				if (x - X > 10) { //左滑
					event.preventDefault();
					this.className = "swipeleft"; //左滑展开
					expansion = this;
				}
				swipeY = false;
			}
			// 上下滑动
			if (swipeY && Math.abs(X - x) - Math.abs(Y - y) < 0) {
				swipeX = false;
			}
		});
	}
}

getItem = (e) =>{
	setStorageValue('address',e);
	navigateTo(`edit_address.html`)
}

getDeleteItem = (id,e) => {
	//$(e).remove();
	// showActionDialog('','您确定要删除吗？',res => {
		
	// })
	let subLoading = layer.open({
		type: 2,
		shadeClose: false,
		content: '正在删除...'
	});
	
	setTimeout(() => {
		layer.close(subLoading);
		showToast('删除成功！');
	}, 500)
}
